/*
 * @Author: chengsl
 * @Date: 2022-04-18 08:55:03
 * @LastEditors: chengsl
 * @LastEditTime: 2023-02-10 13:36:11
 * @Description: file content
 */
const path = require('path')
const dayjs = require('dayjs')
const resolve = (dir) => {
  return path.join(__dirname, dir)
}
const { codeInspectorPlugin } = require('code-inspector-plugin');

module.exports = {
  productionSourceMap: false,
  assetsDir: 'static',
  publicPath: process.env.NODE_ENV === 'development'
    ? '/'
    : '/cxysl-ui',
  runtimeCompiler: true,
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@', resolve('src'))
      .set('src', resolve('src'))
      .set('assets', resolve('src/assets'))
      .set('pages', resolve('src/pages'))
      .set('components', resolve('src/components'))
		config.module
      .rule('js')
      .use('babel-loader')
      .loader('babel-loader')
      .tap(options => {
        // 修改 Babel 配置选项
        return options;
      });

      // 定位代码文件
      config.plugin('code-inspector-plugin').use(
        codeInspectorPlugin({
          bundler: 'webpack',
        })
      );

			const date = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss') // new Date().toLocaleString()
			const version = require('./package.json').version
			// 获取git版本
			let fs = require('fs')
			let gitHEAD = fs.readFileSync('.git/HEAD', 'utf-8').trim() // ref: refs/heads/develop
			let gitBranch
			if (gitHEAD.split('/').length > 2) {
				gitBranch = gitHEAD.split('/')[2] // 环境：develop
			} else {
				let gitFETCH_HEAD = fs.readFileSync('.git/FETCH_HEAD', 'utf-8').trim() // ref: refs/heads/develop
				gitBranch = gitFETCH_HEAD.split(gitHEAD)[1].split('\' of ')[0].split('branch \'')[1]
			}
			config.plugin('html').tap((args) => {
				args[0] = {
					...args[0],
					meta: {
						content: `BuildTime:${date}，Version:${version}，GitBranch:${gitBranch}`
					}
				}
				return args
			})
  },
	configureWebpack: () => {
		const config = {}
		if (process.env.NODE_ENV !== 'development') {
			config.output = {
				filename: `./static/js/[name].[chunkhash].js`,
				chunkFilename: `./static/js/[id].[chunkhash].js`
			}
		} else {
			config.plugins = [require('unplugin-vue-setup-extend-plus/webpack').default()]
		}
		return config
  },
  css: {
    extract: { // 打包后css文件名称添加时间戳
      filename: `./static/css/[name].[chunkhash].css`,
      chunkFilename: `./static/css/[id].[chunkhash].css`
    }
  },
  devServer: {
    disableHostCheck: true,
    // host: '192.169.8.130',
    // host: '0.0.0.0',
    // https: false,
    port: 8080,
    // hot: true,
    // public: '0.0.0.0'
		proxy: {
      '/nodeApi': {
        // target: 'http://192.168.8.130:9527',
        target: 'http://127.0.0.1:9527',
        ws: true,
        logLevel: 'debug',
        changeOrigin: true,
        pathRewrite: {
          '/nodeApi': '/'
        }
      },
		}
  }
}
